﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SZ.Zmotion
{
    public partial class FrmMain : Form
    {
        ZMotionHelper helper = new ZMotionHelper();
        public FrmMain()
        {
            InitializeComponent();

            var ips = helper.GetBoardIPList();
            if (ips != null && ips.Count > 0)
            {
                cmb_ips.DataSource = ips;
            }
        }

        private void btn_Conn_Click(object sender, EventArgs e)
        {
            helper = new ZMotionHelper(cmb_ips.Text);
            bool isConn = helper.Connect();
            if (isConn)
            {
                MessageBox.Show("连接成功。");
            }
            else
            {
                MessageBox.Show("连接失败！");
            }
            //int err = zmcaux.ZAux_OpenEth(txt_IpAddress.Text, out g_handle);
            ////if (err == 0 && g_handle != (IntPtr)0)
            //if (err == 0 && g_handle != IntPtr.Zero)
            //{
            //    MessageBox.Show("连接成功。");
            //}
            //else
            //{
            //    MessageBox.Show("连接失败！");
            //}
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            helper.Close();
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            List<string> ipList = helper.GetBoardIPList();
            if (ipList.Count > 0)
            {
                cmb_ips.DataSource = ipList;
            }
            else
            {
                MessageBox.Show("未找到板卡IP地址。");
            }
        }

        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_JOG_MouseDown(object sender, MouseEventArgs e)
        {
            //轴号;方向   方向0表示正，1标识负
            if (sender is Button button)
            {
                if (button.Tag != null && button.Tag.ToString().Length > 0)
                {
                    if (button.Tag.ToString().Contains(";"))
                    {
                        string[] values = button.Tag.ToString().Split(';');
                        if (values.Length == 2)
                        {
                            short axis = Convert.ToInt16(values[0]);
                            bool dir = values[1] == "1";
                            //连续运动
                            helper.VMove(axis, 30, dir, 0, 500, 500, 100);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 鼠标抬起，停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_JOG_MouseUp(object sender, MouseEventArgs e)
        {
            //轴号;方向
            if (sender is Button button)
            {
                if (button.Tag != null && button.Tag.ToString().Length > 0)
                {
                    if (button.Tag.ToString().Contains(";"))
                    {
                        string[] values = button.Tag.ToString().Split(';');
                        if (values.Length == 2)
                        {
                            short axis = Convert.ToInt16(values[0]);
                            bool dir = values[1] == "1";
                            //停止
                            helper.StopAsix(axis);
                        }
                    }
                }
            }
        }
    }
}